/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jpa.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 *
 * @author isaias
 */
@Entity(name = "usertable")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    
    @Id
    @SequenceGenerator(name = "usertable_gen", sequenceName = "usertable_id_seq", allocationSize = 1)
    @GeneratedValue(generator = "usertable_gen")
    @Column(name = "id")
    private Long id;

    private String name;
    @Column(unique = true)
    private String login;
    private String password;
    private String RG;
    private String CPF;
    private String email;
    private String phone;
    @Temporal(TemporalType.TIMESTAMP)
    private Date last_access;
    private boolean isManager; //se é dono
    private boolean isAdministrator; // usado apenas para administradores do sistema
    private boolean active;
    private boolean isDeleted = false;

    @JoinTable(joinColumns = {
        @JoinColumn(name = "user_id", referencedColumnName = "id")},
    inverseJoinColumns = {
        @JoinColumn(name = "obra_id", referencedColumnName = "id")})
    @ManyToMany
    private List<Obra> obras = new ArrayList<Obra>(); // Usuario pode ter  permisão para varias obras

   @JoinTable(joinColumns = {
        @JoinColumn(name = "user_id", referencedColumnName = "id")},
    inverseJoinColumns = {
        @JoinColumn(name = "permissions_id", referencedColumnName = "id")})
    @ManyToMany
    private List<Permissions> permissions = new ArrayList<Permissions>();

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCPF() {
        return CPF;
    }

    public void setCPF(String CPF) {
        this.CPF = CPF;
    }

    public String getRG() {
        return RG;
    }

    public void setRG(String RG) {
        this.RG = RG;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email.toUpperCase();
    }

    public boolean isIsDeleted() {
        return isDeleted;
    }

    public void setIsDeleted(boolean isDeleted) {
        this.isDeleted = isDeleted;
    }
    
    public boolean isIsAdministrator() {
        return isAdministrator;
    }

    public void setIsAdministrator(boolean isAdministrator) {
        this.isAdministrator = isAdministrator;
    }

    public boolean isIsManager() {
        return isManager;
    }

    public void setIsManager(boolean isManager) {
        this.isManager = isManager;
    }

    public Date getLast_access() {
        return last_access;
    }

    public void setLast_access(Date last_access) {
        this.last_access = last_access;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login.toUpperCase();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name.toUpperCase();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public List<Permissions> getPermissions() {
        return permissions;
    }

    public void setPermissions(List<Permissions> permissions) {
        this.permissions = permissions;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public List<Obra> getObras() {
        return obras;
    }

    public void setObras(List<Obra> obras) {
        this.obras = obras;
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof User)) {
            return false;
        }
        User other = (User) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return getName();
    }

}
